package com.fingersoft.im.ui


import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.ViewInteraction
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.test.runner.AndroidJUnit4
import com.fingersoft.im.R
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsInstanceOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class SplashActivityTest {

    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(SplashActivity::class.java)

    @Rule
    @JvmField
    var mGrantPermissionRule =
            GrantPermissionRule.grant(
                    "android.permission.ACCESS_FINE_LOCATION",
                    "android.permission.ACCESS_COARSE_LOCATION",
                    "android.permission.READ_PHONE_STATE",
                    "android.permission.WRITE_EXTERNAL_STORAGE")

    @Test
    fun splashActivityTest() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        Thread.sleep(4000)
        //show image
        val imageView = onView(
                allOf(withId(R.id.imageview),
                        childAtPosition(
                                withParent(withId(R.id.viewpager)),
                                0),
                        isDisplayed()))
        imageView.check(matches(isDisplayed()))

        testWelcome(imageView)

//        testLogin()
    }

    private fun testLogin() {
        //on login
        val button = onView(
                allOf(withId(R.id.btn_left), withText("登录"),
                        childAtPosition(
                                allOf(withId(R.id.layout_head),
                                        childAtPosition(
                                                withId(R.id.base_layout),
                                                0)),
                                0),
                        isDisplayed()))
        button.perform(click())

        val editTextField = onView(
                allOf(withId(R.id.user_name),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.layout_container),
                                        0),
                                1),
                        isDisplayed()))
        editTextField.perform(replaceText("liubinbin"), closeSoftKeyboard())


        val editTextField4 = onView(
                allOf(withId(R.id.password),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.layout_container),
                                        0),
                                2),
                        isDisplayed()))
        editTextField4.perform(replaceText("fingersoft123"))

        val editTextField5 = onView(
                allOf(withId(R.id.password), withText("fingersoft123"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.layout_container),
                                        0),
                                2),
                        isDisplayed()))
        editTextField5.perform(closeSoftKeyboard())

        val button2 = onView(
                allOf(withId(R.id.submit), withText("登录"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.layout_container),
                                        0),
                                4),
                        isDisplayed()))
        button2.perform(click())

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        Thread.sleep(2000)

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        Thread.sleep(4000)

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        Thread.sleep(7000)

        //check loading contact
        val view2 = onView(
                allOf(withId(R.id.gifView),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.instanceOf(LinearLayout::class.java),
                                        0),
                                0),
                        isDisplayed()))
        view2.check(matches(isDisplayed()))
    }

    private fun testWelcome(imageView: ViewInteraction, next: Boolean = false) {
        //swipe left & enter
        imageView.perform(swipeLeft())
        Thread.sleep(1000)

        imageView.perform(swipeLeft())
        Thread.sleep(1000)
        imageView.perform(swipeLeft())
        Thread.sleep(1000)


        val view = onView(
                allOf(withId(R.id.enter),
                        childAtPosition(
                                withParent(withId(R.id.viewpager)),
                                1),
                        isDisplayed()))

        view.check(matches(isDisplayed()))

        if (next) {
            view.perform(click())
            Thread.sleep(1000)
        }
    }

    private fun childAtPosition(
            parentMatcher: Matcher<View>, position: Int): Matcher<View> {

        return object : TypeSafeMatcher<View>() {
            override fun describeTo(description: Description) {
                description.appendText("Child at position $position in parent ")
                parentMatcher.describeTo(description)
            }

            public override fun matchesSafely(view: View): Boolean {
                val parent = view.parent
                return parent is ViewGroup && parentMatcher.matches(parent)
                        && view == parent.getChildAt(position)
            }
        }
    }
}
